package com.czh.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/demo1")
public class Servlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //第一句 就是设置编码
////        req.setCharacterEncoding("utf-8");
////        resp.setContentType("text/html;charset=utf-8");
//        Cookie cookie=new Cookie("name","柴志浩");
//        resp.addCookie(cookie);
//        resp.getWriter().write("是是是");
        //发送Cookie
        String value = "张三";
        value = URLEncoder.encode(value, "UTF-8");
        Cookie cookie = new Cookie("username",value);
        //设置存活时间   ，1周 7天
        cookie.setMaxAge(60*60*24*7);
        //2. 发送Cookie，response
        resp.addCookie(cookie);


    }

}